package gist

object Dance extends App {

  def findWithPriority2[T](xs: Seq[T], high: T => Boolean, low: T => Boolean): Option[T] = {
    val iter = xs.toIterator
    var found: Option[T] = None
    while (iter.hasNext) {
      val next = iter.next()
      if (high(next)) return Some(next)
      if (found.isEmpty && low(next)) found = Some(next)
    }
    found
  }

  assert(findWithPriority(List(1, 3, 4, 2), (x: Int) => x > 3, (x: Int) => x > 2) == Some(4))
  assert(findWithPriority(List(1, 4, 3, 2), (x: Int) => x > 2, (x: Int) => x > 3) == Some(4))
  assert(findWithPriority(List(1, 3, 4, 2), (x: Int) => x > 3, (x: Int) => x > 4) == Some(4))
  assert(findWithPriority(List(1, 3, 4, 5, 2), (x: Int) => x > 5, (x: Int) => x > 3) == Some(4))
  assert(findWithPriority(List(1, 3, 4, 2), (x: Int) => x > 4, (x: Int) => x > 4) == None)

  def findWithPriority[T](xs: Seq[T], high: T => Boolean, low: T => Boolean): Option[T] = {
    xs.find(high) match {
      case None => xs.find(low)
      case v @ _ => v
    }
  }

  def Time[A](fn: => A) {
    val time = System.currentTimeMillis()
    println(fn)
    println(System.currentTimeMillis() - time)
  }
  Time(findWithPriority(List(1, 3, 4, 2), (x: Int) => x > 3, (x: Int) => x > 2) == Some(4))
  Time(findWithPriority2(List(1, 3, 4, 2), (x: Int) => x > 3, (x: Int) => x > 2) == Some(4))
  
  
  var map = Map[String,String]()
  
 map = Map("1" -> "1")
  
}